﻿*******************************************************************************
***
*** SPSS syntax to merge the individual-level file (MASTER) with separate 
*** country/survey files (PLUGs).
***
*******************************************************************************

***
*** set working directory containing MASTER, PLUG_COUNTRY and PLUG_SURVEY files
*** e.g. cd "C:\"
***

file handle WORKDIR /name ='C:\' .

***
*** open MASTER file
***

get file="WORKDIR\MASTER.sav".
dataset name MASTER. 

***
*** merge MASTER and PLUG_COUNTRY files
***

get file="WORKDIR\PLUG_COUNTRY.sav".
sort cases by T_COUNTRY.
dataset name PLUG_COUNTRY.

dataset activate MASTER.
string T_COUNTRY(A6).
compute T_COUNTRY = T_COUNTRY_L2U.
execute.
sort cases by T_COUNTRY.
match files /file=* /table=PLUG_COUNTRY /by T_COUNTRY.
execute.
delete variables T_COUNTRY.
dataset close PLUG_COUNTRY.

***
*** merge MASTER and PLUG_SURVEY files
***

get file="WORKDIR\PLUG_SURVEY.sav".
sort cases by T_PROJECT_NAME T_PROJECT_WAVE T_COUNTRY_L1U T_COUNTRY_L2U.
dataset name PLUG_SURVEY.

dataset activate MASTER.
sort cases by T_PROJECT_NAME T_PROJECT_WAVE T_COUNTRY_L1U T_COUNTRY_L2U.
match files /file=* /table=PLUG_SURVEY /by  T_PROJECT_NAME T_PROJECT_WAVE T_COUNTRY_L1U T_COUNTRY_L2U.
execute.
dataset close PLUG_SURVEY.

***
*** save the resulting file
***

save outfile='WORKDIR\MASTER-merged.sav'.
